import {
  examinesPreviewFiledNameAPI,
  examinesPreviewExamineFlowAPI
} from '/src/api/examine/index.js'
import { isEmpty } from '/src/utils/types.js'
import { debounce } from 'throttle-debounce'

/**
 * debouncedGetWkFlowList
 * initWkFlowData
 * validateWkFlowData
 */

export function useWkApprovalFlowApply() {

  const approvalFlowOption = reactive({
    // 审批信息
    _wkFlowSetFields: null, // 有值有审批流
    _wkFlowSetFieldNames: [],
    _wkFlowParams: {},
    _wkFlowListParams: {},
    _wkFlowResolve: null,
    _wkFieldForm: {} // 自定字段值
  })
  const debouncedGetWkFlowList = debounce(500, _getWkFlowList)
  // function created() {
  //   debouncedGetWkFlowList = debounce(500, _getWkFlowList)
  // }

  /**
   * 初始化审批流
   * data 包含 params 参数
   * fieldForm 自定义字段值
   */
  function initWkFlowData(data, resultFun) {
    if (data.params) {
      approvalFlowOption._wkFlowParams = data.params
    }
    if (data.fieldForm) {
      approvalFlowOption._wkFieldForm = data.fieldForm
    }
    approvalFlowOption._wkFlowResolve = resultFun
    _getWkFlowSetFields()
  }

  /**
   * 获取可设置字段
   */
  function _getWkFlowSetFields() {
    examinesPreviewFiledNameAPI(approvalFlowOption._wkFlowParams).then(res => {
      approvalFlowOption._wkFlowSetFields = res.data.data
      if (approvalFlowOption._wkFlowSetFields) {
        approvalFlowOption._wkFlowSetFieldNames = approvalFlowOption._wkFlowSetFields.map(item => item.fieldName)
        _getWkFlowList(null, approvalFlowOption._wkFieldForm)
      } else {
        if (approvalFlowOption._wkFlowResolve) {
          approvalFlowOption._wkFlowResolve({
            list: null
          })
        }
      }
    }).catch(() => {})
  }

  /**
   * 获取审批流展示字段
   */
  function _getWkFlowList(field, fieldForm) {
    // 不存在 不监测
    if (!approvalFlowOption._wkFlowSetFields) {
      return
    }
    if (approvalFlowOption._wkFlowSetFieldNames.includes(field) || !field) {
      const params = {}
      approvalFlowOption._wkFlowSetFieldNames.forEach(key => {
        const value = fieldForm ? fieldForm[key] : ''
        if (Array.isArray(value)) {
          params[key] = value.join(',')
        } else {
          params[key] = isEmpty(value) ? '' : value
        }
      })
      approvalFlowOption._wkFlowListParams = {
        ...approvalFlowOption._wkFlowParams,
        dataMap: params
      }
      examinesPreviewExamineFlowAPI(approvalFlowOption._wkFlowListParams).then(res => {
        const resData = res.data.data || {}

        const wkFlowList = resData.examineFlowList || []
        wkFlowList.forEach(item => {
          item.values = []
        })
        if (approvalFlowOption._wkFlowResolve) {
          approvalFlowOption._wkFlowResolve({
            list: wkFlowList,
            resData: resData
          })
        }
      }).catch(() => {})
    }
  }

  /**
   * 验证数据是否完整，并反馈结果
   */
  function validateWkFlowData(flowList) {
    let pass = true
    const list = []

    if (!flowList) {
      return {
        pass,
        data: null
      }
    }
    flowList.forEach(item => {
      if (item.examineType === 4) {
        if (item.values.length > 0) {
          list.push({
            flowId: item.flowId,
            userList: item.values.map(item => item.userId)
          })
        } else {
          pass = false
        }
      }
    })

    return {
      pass,
      data: {
        ...approvalFlowOption._wkFlowListParams,
        optionalList: list
      }
    }
  }

  return {
    debouncedGetWkFlowList,
    approvalFlowOption,
    initWkFlowData,validateWkFlowData
  }
}

